package com.orcsystem.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orcsystem.common.utils.PageUtils;
import com.orcsystem.common.utils.Query;
import com.orcsystem.product.dao.AttrGroupDao;
import com.orcsystem.product.entity.AttrGroupEntity;
import com.orcsystem.product.service.AttrGroupService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // 三级分类ID 为 0  则 不过滤三级分类ID
        String key = (String) params.get("key");     // 前端有搜索 关键字 在过滤关键字
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        // 查询三级分类下
        if (!StringUtils.isEmpty(key)) {
            wrapper = wrapper.and((item) -> {
                item.eq("attr_group_id", key).or().like("attr_group_name",key).or().like(
                        "descript", key);
            });
        }
        if (catelogId == 0){
            // 查询所有
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }else{
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return  new PageUtils(page);

        }
    }

}
